#coding=utf-8
'''
Created on 2013-10-24

@author: Ben

配置管理
'''

import AIConfig
import GameConfig
import Logger
import yaml

_current_module = None
_current_config = None

'''
读取配置
'''
def loadConfig(path):
    if not loadCommonConfig():
        return False
    if path != '' and not loadUserConfig(path):
        return False
    return True

'''
读取公共配置
'''
def loadCommonConfig():
    global _current_module
    _current_module = GameConfig
    #读取game公共配置
    if not _loadItems('conf/game-config.yaml', True, gameItemsLoadFunc):
        return False
    _current_module = AIConfig
    #读取ai公共配置
    if not _loadItems('conf/ai-config.yaml', True, AIItemsLoadFunc):
        return False
    Logger.log('读取公共配置完成', Logger.LOGGER_COMMON, True)
    return True

'''
读取用户配置
'''
def loadUserConfig(userPath):
    global _current_module
    _current_module = GameConfig
    #读取game用户配置
    if not _loadItems(userPath+'conf/game-config.yaml', False, gameItemsLoadFunc):
        return False
    _current_module = AIConfig
    #读取ai用户配置
    if not _loadItems(userPath+'conf/ai-config.yaml', False, AIItemsLoadFunc):
        return False
    Logger.log('读取用户配置完成', Logger.LOGGER_COMMON, True)
    return True

def gameItemsLoadFunc(required):
    _loadItem('ast_site_url', required)
    _loadItem('chrome_user_agent', required)
    _loadItem('encrypted', required)
    _loadItem('game_server_port', required)
    _loadItem('game_server_url', required)
    _loadItem('gcld_site_url', required)
    _loadItem('password', not required)
    _loadItem('reconnect_enabled', required)
    _loadItem('user_name', not required)

def AIItemsLoadFunc(required):
    _loadItem('attack_stop_food', required)
    _loadItem('attack_stop_food_relative', required)
    _loadItem('attack_stop_food_use_min', required)
    _loadItem('attack_generals', required)
    _loadItem('featlist_box_upper_bound', required)
    _loadItem('market_goods_priority', required)
    _loadItem('market_num_upper_bound', required)
    _loadItem('market_precious_goods', required)
    _loadItem('nation_feed_upper_bound', required)
    _loadItem('online_reward_all_allowed', required)
    _loadItem('phantom_start_times', required)
    _loadItem('recruit_token_lower', required)
    _loadItem('recruit_token_upper', required)
    _loadItem('worship_consecutive_times', required)
    _loadItem('worship_start_food', required)

def _loadItem(key, required):
    if required or key in _current_config:
        _current_module.__dict__[key] = _current_config[key]
    
'''
@param url: 
@param required: 配置项是否必须存在
@param itemsLoadFunc: 配置项加载函数
'''
def _loadItems(url, required, itemsLoadFunc):
    try:
        with open(url) as f:
            config = yaml.load(f)
            global _current_config
            _current_config = config
            itemsLoadFunc(required)
    except IOError:
        Logger.log('配置文件' + url + '不存在', Logger.LOGGER_COMMON, True)
        return False
    except KeyError:
        Logger.log('配置文件' + url + '配置项不存在', Logger.LOGGER_COMMON, True)
        return False
    except yaml.scanner.ScannerError:
        Logger.log('配置文件' + url + '格式错误', Logger.LOGGER_COMMON, True)
        return False
    except:
        raise
    return True

if __name__ == '__main__':
    pass